﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Tsanie.ShellExt.Definitions;

namespace Tsanie.ShellExt
{
    [Guid("D2367552-6A58-4621-9A56-2C549E8D215D")]
    [ComVisible(true)]
    public class ManagerPropertySheet : PropertySheetBase
    {
        PropertySheetTemplate _template = null;
        PropertySheetBaseProvider _provider = null;

        protected override void OnInitFiles()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                string[] files = new string[this.Files.Count];
                for (int i = 0; i < this.Files.Count; i++)
                {
                    files[i] = this.Files[i];
                }
                _template = _provider.GetTemplate(files);
            }
#if DEBUG
            else
            {
                foreach (string file in this.Files)
                {
                    Common.OutputDebugString("OnInitFiles():: Can't find the provider... {0}", file);
                }
            }
#endif
        }
        protected override PropertySheetTemplate OnGetTemplate()
        {
            if (_template == null)
            {
                OnInitFiles();
            }
            return _template;
        }

        protected override bool AllowMultiFiles
        {
            get
            {
                if (_provider != null)
                {
                    return _provider.AllowMultiFiles;
                }
                return false;
            }
        }

        protected override void OnCreatePage()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                _provider.OnCreatePage();
            }
        }
        protected override NotifyResult OnCancel()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                return _provider.OnCancel();
            }
            return NotifyResult.NoError;
        }
        protected override NotifyResult OnActivate()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                return _provider.OnActivate();
            }
            return NotifyResult.NoError;
        }
        protected override NotifyResult OnApply()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                return _provider.OnApply();
            }
            return NotifyResult.NoError;
        }
        protected override NotifyResult OnShowHelp()
        {
            _provider = Manager.GetCacheProvider<PropertySheetBaseProvider>(this.Files);
            if (_provider != null)
            {
                return _provider.OnShowHelp();
            }
            return NotifyResult.NoError;
        }

    }
}
